package com.nuoniu.sibanyun.controller.crm;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.crm.CrmGoods;
import com.nuoniu.sibanyun.entity.crm.CrmGoodsOrder;
import com.nuoniu.sibanyun.service.crm.ICrmGoodsOrderService;
import com.nuoniu.sibanyun.service.crm.ICrmGoodsService;
import com.nuoniu.sibanyun.service.expenses.IExpensesBusinessOrderService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * <p>
 * 商品订单 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-07-17
 */
@RestController
@RequestMapping("/member/order")
@Api(tags = "商品订单")
public class CrmGoodsOrderController extends BaseController {

    @Autowired
    private ICrmGoodsOrderService orderService;
    @Autowired
    private ICrmGoodsService goodsService;


    /**
     * 商品订单列表
     * @author 小啊刚
     * @date 2020-07-21
     */
    @ApiOperation("商品订单列表")
    @GetMapping("page")
    public HttpResult pageOrder(Page page, CrmGoodsOrder order) {
        return HttpResult.ok(orderService.pageOrder(page, order));
    }

    /**
     * 获取我的下级订单
     * @author 小啊刚
     * @date 2022-05-27
     */
    @ApiOperation("获取我的下级订单")
    @GetMapping("getShareOrderList")
    public HttpResult getShareOrderList() {

        return HttpResult.ok();
    }

    /**
     * 购买商品订单
     * @author 小啊刚
     * @date 2020-07-21
     */
    @ApiOperation("购买商品订单")
    @PostMapping("save")
    public HttpResult saveOrder(@RequestBody CrmGoodsOrder order) {
        orderService.saveOrder(order);
        return HttpResult.ok(order);
    }

    /**
     * 续费订单
     * @author 小啊刚
     * @date 2020-07-23
     */
    @ApiOperation("续费订单")
    @PostMapping("saveRenew")
    public HttpResult saveRenew(@RequestBody CrmGoodsOrder order) {
        if(order.getUsers() == 0 && order.getYears() == 0) {
            throw new NuoNiuException("支付失败，续费年限和用户数不能同时为0");
        }

        orderService.saveRenew(order);

        return HttpResult.ok(order);
    }

    /**
     * 获取订单详情
     * @author 小啊刚
     * @date 2020-08-22
     */
    @ApiOperation("获取订单详情")
    @GetMapping("getOrderDetail")
    public HttpResult getOrderDetail(String orderId) {
        CrmGoodsOrder order = orderService.getById(orderId);
        return HttpResult.ok(order);
    }

    /**
     * 删除订单
     * @author 小啊刚
     * @date 2020-08-24
     */
    @ApiOperation("删除订单")
    @GetMapping("delete")
    public HttpResult delete(String id) {
        CrmGoodsOrder order = orderService.getById(id);
        if(null != order && order.getOrderStatus() > 0) {
            return HttpResult.ok("已支付订单，不能删除");
        }
        orderService.removeById(id);
        return  HttpResult.ok();
    }

}
